"""
encoding = 'utf-8'
@author : ffine
@time   : 2022-05-04  16:28
@IDEA   : PyCharm
@goal   : 迭代器(iterator) 生成器(generator) 装饰器(decorator)
"""
import time
from datetime import datetime

print(f"{'迭代器':=^50s}")

mylist = [1, 2, 3, 4, 5]
for i in mylist:
	print(i)

it = iter(mylist)
for x in range(5):
	print(next(it))

# 判断一个对象是否是可迭代对象
from collections.abc import Iterable

print(isinstance('abc', Iterable))
print(isinstance(mylist, Iterable))
print(isinstance(it, Iterable))
print(isinstance(123, Iterable))

print(f"{'生成器':=^50s}")


# 生成器: 边迭代边输出 可以让我们创建一个惰性计算的序列, 从而节省内存
def fib(max_num):
	n, a, b = 0, 0, 1
	while n < max_num:
		yield b     # 将b返回给调用者, 并且函数会暂停执行, 等到下一次调用next(), 再次返回b
		a, b = b, a + b
		n = n + 1
	return 'done'


f = fib(1000)
print(type(f))
for i in f:
	if i > 2000:
		break
	print(i)
# print(f.__next__())


print(f"{'装饰器':=^50s}")


# 装饰器: 可以动态的为函数添加功能, 可以增加函数的功能, 也可以增加函数的执行速度
def log(func):
	def wrapper(*args, **kw):
		print('call %s():' % func.__name__)
		return func(*args, **kw)
	
	return wrapper


def time_log(func):
	def wrapper(*args, **kw):
		start_time = datetime.now()
		func(*args, **kw)
		end_time = datetime.now()
		print(f'time:{end_time - start_time}')
	
	return wrapper


@log
@time_log
def now():
	"""
	装饰后,可看作: now = log(now)
	:return:
	"""
	print(f"now function runing...")


now()


